
#include "TextEditWidget.h"

TextEditWidget::TextEditWidget(QTextEdit* resultArea)
{

    this->textMonitoring = new TextMonitoring(this);
    this->textMonitoring->resultField = resultArea;
    this->resultArea = resultArea;

    QObject::connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(cursorPosCh()));
    QObject::connect(this, SIGNAL(textChanged()), this, SLOT(textCh()));
    QObject::connect(this, SIGNAL(selectionChanged()), this, SLOT(selectionCh()));
}

void TextEditWidget::dropEvent(QDropEvent* event)
{
    if(event->mimeData()->hasText())
    {

        this->textMonitoring->sendEvent(DROP, this->textCursor().selectionStart(), this->textCursor().selectionEnd());

        QTextEdit::dropEvent(event);
    }
    else
        QTextEdit::dropEvent(event);
}

void TextEditWidget::cursorPosCh()
{
    this->textMonitoring->sendEvent(CURSOR_POSITION_CHANGED, this->textCursor().selectionStart(), this->textCursor().selectionEnd());
}

void TextEditWidget::textCh()
{
    this->textMonitoring->sendEvent(TEXT_CHANGED, this->textCursor().selectionStart(), this->textCursor().selectionEnd());

    if(textMonitoring->changes->size() > 0)
    {
        this->resultArea->setText(resultArea->toPlainText() + QString::number(textMonitoring->changes->size()) + " "
                              + QString::number(textMonitoring->changes->at(textMonitoring->changes->size()-1)->beginInSrc) + " "
                              + QString::number(textMonitoring->changes->at(textMonitoring->changes->size()-1)->endInSrc) + " "
                              + textMonitoring->changes->at(textMonitoring->changes->size()-1)->value + " "
                              + QString::number(textMonitoring->changes->at(textMonitoring->changes->size()-1)->type) + " "
                                  + textMonitoring->allText->mid(0) + "\n");
    }

}

void TextEditWidget::selectionCh()
{
    this->textMonitoring->sendEvent(SELECTION_CHANGED, this->textCursor().selectionStart(), this->textCursor().selectionEnd());

}
